package com.army.study.comicqixi;

import com.army.study.comicqixi.database.Form;
import com.army.study.comicqixi.database.FormDatabase;
import com.army.study.comicqixi.utils.ComponentProviderUtils;
import com.army.study.comicqixi.utils.DatabaseUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.ability.LocalRemoteObject;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.ComponentProvider;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.*;
import ohos.utils.zson.ZSONObject;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class SendServiceAbility extends Ability {
    private static final int NOTICE_ID = 1005;
    private DatabaseHelper helper = new DatabaseHelper(this);
    private OrmContext connect;

    public class MyRemoteObject extends RemoteObject implements IRemoteBroker {
        public MyRemoteObject(String descriptor) {
            super(descriptor);
            System.out.println("MyRemoteObject Created!");
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
            System.out.println("MyRemoteObject::onRemoteRequest Code: " + code);

            String fourText = data.readString();
            String eightText = data.readString();
            System.out.println("MyRemoteObject::onRemoteRequest Four: " + fourText);
            System.out.println("MyRemoteObject::onRemoteRequest Eight: " + eightText);

            // 更新卡片信息
            updateForms(fourText, eightText);
            // 调用通知提示
            notice();

            return true;
        }
    }

    @Override
    protected void onStart(Intent intent) {
        System.out.println("TimerAbility::onStart");
        connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);

        super.onStart(intent);
    }

    @Override
    protected IRemoteObject onConnect(Intent intent) {
        System.out.println("SendServiceAbility::onConnect");
        return new MyRemoteObject("myRemoteObject").asObject();
    }

    @Override
    protected void onDisconnect(Intent intent) {
        System.out.println("SendServiceAbility::onDisconnect");
        super.onDisconnect(intent);
    }

    @Override
    protected void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);
    }

    private void notice() {
        // 创建通知
        NotificationRequest request = new NotificationRequest(NOTICE_ID);
        request.setAlertOneTime(true);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setText("动漫七夕服务卡片有更新,请查看");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
        // 绑定通知
        keepBackgroundRunning(NOTICE_ID, request);
    }

    private void updateForms(String firstParam, String secondParam) {
        // 从数据库中获取卡片信息
        OrmPredicates ormPredicates = new OrmPredicates(Form.class);
        List<Form> formList = connect.query(ormPredicates);
        // 更新卡片文字
        if (formList.size() <= 0) {
            return;
        }

        ZSONObject zsonObject = new ZSONObject();
        zsonObject.put("fourText", firstParam);
        zsonObject.put("eightText", secondParam);

        for (Form form : formList) {
            // 遍历卡片列表更新卡片
            ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(form, this, zsonObject);

            try {
                Long updateFormId = form.getFormId();
                updateForm(updateFormId, componentProvider);
            } catch (FormException e) {
                // 删除不存在的卡片
                DatabaseUtils.deleteFormData(form.getFormId(), connect);
                System.out.println("[updateForms]onUpdateForm updateForm error");
            }
        }
    }
}
